Skip to content

feat: eliminate LEFT JOINs whose right side is unused#23566

Open
simonvandel wants to merge 2 commits into
apache:mainfrom
simonvandel:push-trmumwlllzop
Open

feat: eliminate LEFT JOINs whose right side is unused#23566
simonvandel wants to merge 2 commits into
apache:mainfrom
simonvandel:push-trmumwlllzop

Conversation

@simonvandel

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • Closes #.

Rationale for this change

Join elimination is useful in e.g. generated queries or views, where joined columns can end up not being used.

An explanation of when the optimization is valid, can be seen in the docs update for EliminateJoin.

What changes are included in this PR?

Builds upon the analysis that #22652 introduced for the EliminateJoin optimization pass. We use it to detect when the right-side of a left-join can be removed.

First commit adds test cases.
Second commit adds the optimization change, and shows how it affects the added tests.

Are these changes tested?

Yes, SLT additions that test the feature end-to-end.

Are there any user-facing changes?

Yes, faster queries!

claude added 2 commits July 14, 2026 14:12
Extend the `EliminateJoin` rule to remove a LEFT JOIN entirely
(replacing it with its left input) when:

1. none of the right side's columns are referenced above the join, and
2. the join cannot multiply left rows: either the right side is provably
   unique on the equi-join keys (PRIMARY KEY / UNIQUE constraints,
   GROUP BY, DISTINCT), or the join's ancestors are
   duplicate-insensitive.

A LEFT JOIN preserves every left row whether or not it matches, so under
these conditions the join has no observable effect. A join filter does
not block the rewrite: for a left join it only decides whether a row is
matched or null-padded, and either way the row is emitted. Such joins
commonly appear in generated SQL and in queries over views that join in
lookup tables the query does not read.

This reuses the live-column tracking, duplicate-insensitivity context,
and join-key uniqueness analysis that `EliminateJoin` already uses to
rewrite inner joins to semi joins.

Includes sqllogictest coverage for the eliminated and preserved cases,
including result correctness for unmatched and duplicated rows.
@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Jul 14, 2026

@neilconway neilconway left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this -- lgtm overall!

Nit: The PR description will be the git commit message when this is squash-merged, so it's confusing to talk about "first commit" vs "second commit" there.

The existing analysis has both unit tests and SLTs; can you take a look at whether any of the existing unit tests should be extended to cover outer joins?

// be replaced by its left input. A join filter cannot prevent this: it
// only decides whether a left row is matched or null-padded, and either
// way the row is emitted.
if join.join_type == JoinType::Left

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about supporting RIGHT JOINs as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, would you be okay with this being a follow-up task? Then we could land the left-join support faster

Comment on lines +418 to +423
&& (duplicate_insensitive
|| side_unique_on_join(
join.right.schema(),
join.on.iter().map(|(_, right)| right),
join.null_equality,
))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider refactoring this into a helper, so we can use it here and also in rewritten_join_type?

reset datafusion.execution.batch_size;

##########
# Eliminate unused LEFT JOINs (`EliminateJoin` rule)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add test cases for LIMIT and LEFT JOIN LATERAL?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants